package com.itheima.appumeng.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.itheima.appumeng.R;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Random;

public class DetailActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_detail);
        initView();
    }

    private void initView() {
        Button btn01 = (Button) findViewById(R.id.btn_01);
        Button btn02 = (Button) findViewById(R.id.btn_02);
        Button btn03 = (Button) findViewById(R.id.btn_03);
        btn01.setOnClickListener(mOnClickListener);
        btn02.setOnClickListener(mOnClickListener);
        btn03.setOnClickListener(mOnClickListener);
    }

    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_01:
                    onBtn01();
                    break;
                case R.id.btn_02:
                    onBtn02();
                    break;
                case R.id.btn_03:
                    onBtn03();
                    break;
            }
        }
    };

    private void onBtn01() {
        //enterFriendActivity
        MobclickAgent.onEvent(this,"enterFriendActivity");

    }

    private void onBtn02() {
        //假数据
        String[] singers = new String[]{
                "张学友","黎明","郭富城"
        };
        String[] songs = new String[]{"李香兰","两个人的烟火","樱之花"};
        int index = new Random().nextInt(singers.length);
        String singer = singers[index];
        String song = songs[index];
        HashMap<String ,String> map = new HashMap<>();
        map.put("singer",singer);
        map.put("song" ,song);
        //播放音乐
        MobclickAgent.onEvent(this,"playMusic",map);

    }

    private void onBtn03() {
        //giveRedPackets
        int count = new Random().nextInt(10)+10;
        //计算统计：发红包，平均金额
        MobclickAgent.onEventValue(this,"giveRedPackets2",null,count);

    }

    @Override
    protected void onResume() {
        super.onResume();
       // MobclickAgent.onPageStart("DetailActivity");//统计页面跳转
        //这样可以动态获取activnty的名字
        MobclickAgent.onPageStart(getClass().getSimpleName());//统计页面跳转

        MobclickAgent.onResume(this);       //  统计界面停留时长
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());  //仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息。"SplashScreen"为页面名称，可自定义
        MobclickAgent.onPause(this);
    }
}
